package cn.itcast.service.impl;

import cn.itcast.pojo.info;
import cn.itcast.pojo.infoExample;
import cn.itcast.service.InfoService;
import com.github.pagehelper.PageHelper;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;
import java.util.List;

/**
 * @author 辉
 * @title: UserServiceImpl
 * @projectName wx_shop
 * @description: TODO
 * @date 2019/6/1716:27
 */
@Slf4j
@Service
public class InfoServiceImpl implements InfoService {

    @Resource
    cn.itcast.mapper.infoMapper infoMapper;


    @Override
    public List<info> findAllInfo(String openId) {
        infoExample iEx = new infoExample();
        infoExample.Criteria criteria = iEx.createCriteria();
        criteria.andOpenidEqualTo(openId);
        iEx.setOrderByClause("info_time DESC");
        PageHelper.startPage(1,60);

        return infoMapper.selectByExample(iEx);
    }

    @Override
    public List<info> findTypeInfo(String openId, String type) {
        infoExample iEx = new infoExample();
        infoExample.Criteria criteria = iEx.createCriteria();
        criteria.andInfoTypeEqualTo(type);
        if(!type.equals("系统通知")&&!type.equals("最新活动")){
            criteria.andOpenidEqualTo(openId);
            PageHelper.startPage(1,50);
        }
        iEx.setOrderByClause("info_time DESC");

        return infoMapper.selectByExample(iEx);
    }

    @Override
    public int findNumRead(String openId) {
        infoExample iEx = new infoExample();
        infoExample.Criteria criteria = iEx.createCriteria();
        criteria.andOpenidEqualTo(openId);
        criteria.andInfoReadEqualTo("0");
        iEx.setOrderByClause("info_time DESC");
        List<info> infos = infoMapper.selectByExample(iEx);
        return infos.size();
    }

    @Override
    public boolean overRead(String id) {
        info info = new info();
        info.setId(Integer.parseInt(id));
        info.setInfoRead("1");
        int i = infoMapper.updateByPrimaryKeySelective(info);
        return i==1;
    }

    @Override
    public boolean readMessage(String openId) {
        return infoMapper.readMessage(openId) >= 0;
    }
}
